/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cpasl;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Rectangle2D;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.shape.Rectangle;
import javafx.stage.Screen;

/**
 *
 * @author seanm_000
 */
public class PartTwoVideoGroup extends VideoGroup {
    
    private final static int LABEL_Y_OFFSET = 40;
    
    private Label labelA = new Label("A");
    
    private Label labelB = new Label("B");
    
    private Label labelUnknown = new Label("?");
    
    private Rectangle rectangleA = null;
    
    private Rectangle rectangleB = null;
    
    private Rectangle rectangleUnknown = null;
    
    private int aX = 0;
    
    private int bX = 0; 
    
    private VideoChoiceBlank videoChoiceBlank = new VideoChoiceBlank();
    
    private Groups next = Groups.VIDEO_A;
    
    private Button videoAButton = new Button("Video A");
    
    private Button videoBButton = new Button("Video B");
    
    public PartTwoVideoGroup() {
        Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
        aX = (int) (primaryScreenBounds.getWidth() * 0.25) - (VIDEO_WIDTH/2);
        bX = (int) (primaryScreenBounds.getWidth() * 0.75) - (VIDEO_WIDTH/2);
        labelA.setTranslateX((primaryScreenBounds.getWidth() * 0.25) - (labelA.getPrefWidth()/2));
        labelA.setTranslateY(centerY-LABEL_Y_OFFSET);
        rectangleA = new Rectangle(aX, centerY, VIDEO_WIDTH, VIDEO_HEIGHT);
        labelB.setTranslateX((primaryScreenBounds.getWidth() * 0.75) - (labelB.getPrefWidth()/2));
        labelB.setTranslateY(centerY-LABEL_Y_OFFSET);
        rectangleB = new Rectangle(bX, centerY, VIDEO_WIDTH, VIDEO_HEIGHT);
        labelUnknown.setTranslateX((primaryScreenBounds.getWidth() * 0.5) - (labelUnknown.getPrefWidth()/2));
        labelUnknown.setTranslateY(centerY-LABEL_Y_OFFSET);
        rectangleUnknown = new Rectangle(centerX, centerY, VIDEO_WIDTH, VIDEO_HEIGHT);    
        getChildren().add(labelA);
        getChildren().add(rectangleA);
        getChildren().add(labelB);
        getChildren().add(rectangleB);
        getChildren().add(labelUnknown);
        getChildren().add(rectangleUnknown);
        
        videoAButton.setId("video-button");
        videoAButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                CpaslManager.getInstance().setVideoASelected();
                CpaslManager.getInstance().writeVideoChoice();
                CpaslManager.getInstance().switchToDisplay(next, 1000);
            }
            
        });
        videoAButton.setTranslateX(aX);
        videoAButton.setTranslateY(centerY + VIDEO_HEIGHT + 10);
        videoAButton.setVisible(false);
        getChildren().add(videoAButton);
        
        videoBButton.setId("video-button");
        videoBButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                CpaslManager.getInstance().setVideoBSelected();
                CpaslManager.getInstance().writeVideoChoice();
                CpaslManager.getInstance().switchToDisplay(next, 1000);
            }
            
        });
        videoBButton.setTranslateX(bX);
        videoBButton.setTranslateY(centerY + VIDEO_HEIGHT + 10);
        videoBButton.setVisible(false);
        getChildren().add(videoBButton);
    }
    
    public void displayA() {
        videoAButton.setVisible(false);
        videoBButton.setVisible(false);
        mediaView.setX(aX);
        mediaView.setY(centerY);
        
        mediaPlayer.play();
        mediaPlayer.setOnEndOfMedia(new Runnable() {

            @Override
            public void run() {
                mediaPlayer.stop();
                getChildren().remove(mediaView);
                CpaslManager.getInstance().switchToDisplay(Groups.VIDEO_B, videoChoiceBlank, 500);
            }
        });
    }
    
    public void displayB() {
        mediaView.setX(bX);
        mediaView.setY(centerY);
        mediaPlayer.play();
        mediaPlayer.setOnEndOfMedia(new Runnable() {

            @Override
            public void run() {
                mediaPlayer.stop();
                getChildren().remove(mediaView);
                CpaslManager.getInstance().switchToDisplay(Groups.VIDEO_A_OR_B_DISPLAY, videoChoiceBlank, 500);
            }
        });
    }
    
    public void displayAorB() {
        centerVideo();
        mediaPlayer.play();
        mediaPlayer.setOnEndOfMedia(new Runnable() {

            @Override
            public void run() {
                mediaPlayer.stop();
                getChildren().remove(mediaView);
                CpaslManager.getInstance().switchToDisplay(Groups.VIDEO_A_OR_B_CHOICE, videoChoiceBlank, 500);
            }
        });
    }
    
    public void displayChoice() {
        videoAButton.setVisible(true);
        videoBButton.setVisible(true);
    }
    
    /** 
     * Sets what the next thing should be, after the user selects A or B.
     * 
     * @param nextGroups the next scene to display
     */
    public void setGroups(Groups nextGroups) {
        next = nextGroups;
    }
    
}
